package Presenter;

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import Models.InventoryUsage;
import Models.LabType;
import Models.PackageType;
import Models.VesselCleaningProcedure;

public class PackageTypePresenter extends AbstractPresenter<PackageType> {
	final String SINGLEITEM = "packageType";
	final String MULTIITEM = "packageTypes";
	
	public ArrayList<PackageType> tryJSONParseArray(String input){
		return super.doJSONParseArray(input, SINGLEITEM, MULTIITEM);
	}
	
	public PackageType tryJSONParse(String input){
		return super.doJSONParse(input, SINGLEITEM);
	}
	
	public PackageType createItemFromJSONObject(JSONObject jsonObject){
		PackageType item = new PackageType();
		try{
			item.setPackageTypeID(jsonObject.getInt("packageTypeID"));
			item.setPackageDescription(jsonObject.getString("packageTypeDesc"));
			item.setPackageVolume(jsonObject.getDouble("packageTypeVol"));
			item.setNumItems(jsonObject.getInt("numItems"));
			item.setServingVessel(jsonObject.getBoolean("servingVessel"));
			
			//Set Inventory Usage Collection
			ArrayList<InventoryUsage> invUsages = new ArrayList<InventoryUsage>();
			if(!jsonObject.isNull("invUsageCollection")){
				Object itm = jsonObject.get("invUsageCollection");
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						invUsages.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					invUsages.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setInventoryUsages(invUsages);
			}
			
			//Set VCP Collection
			ArrayList<VesselCleaningProcedure> vcps = new ArrayList<VesselCleaningProcedure>();
			if(!jsonObject.isNull("vcpCollection")){
				Object itm = jsonObject.get("vcpCollection");
				VesselCleaningProcedurePresenter presenter = new VesselCleaningProcedurePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						vcps.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					vcps.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setProcedures(vcps);
			}
			
			//Set LabType Collection
			ArrayList<LabType> labTypes = new ArrayList<LabType>();
			if(!jsonObject.isNull("labCollection")){
				Object itm = jsonObject.get("labCollection");
				LabTypePresenter presenter = new LabTypePresenter();
				if(itm instanceof JSONArray){
					JSONArray jPSTArray = (JSONArray)itm;
					for(int i=0; i<jPSTArray.length(); i++){
						JSONObject jsonItem = jPSTArray.getJSONObject(i);
						labTypes.add(presenter.createItemFromJSONObject(jsonItem));
					}
				} else {
					JSONObject jsonItem = (JSONObject)itm;
					labTypes.add(presenter.createItemFromJSONObject(jsonItem));
				}
				item.setLabs(labTypes);
			}
			
			return item;
		} catch(JSONException ex){
			ex.printStackTrace();
		} catch(Exception ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public JSONObject createJSON(PackageType item){
		try{
			JSONObject json = new JSONObject();
			json.put("packageTypeID", item.getPackageTypeID());
			json.put("packageTypeDesc", item.getPackageDescription());
			json.put("packageTypeVol", item.getPackageVolume());
			json.put("numItems", item.getNumItems());
			json.put("servingVessel", item.isServingVessel());
			//Create Doc Collection
			if(item.getInventoryUsages()!=null){
				InventoryUsagePresenter presenter = new InventoryUsagePresenter();
				if(item.getInventoryUsages().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getInventoryUsages().size(); i++){
						jsonArray.put(presenter.createJSON(item.getInventoryUsages().get(i)));
					}
					json.put("invUsageCollection", jsonArray);
				}
			}
			//Create VCP Collection
			if(item.getProcedures()!=null){
				VesselCleaningProcedurePresenter presenter = new VesselCleaningProcedurePresenter();
				if(item.getProcedures().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getProcedures().size(); i++){
						jsonArray.put(presenter.createJSON(item.getProcedures().get(i)));
					}
					json.put("vcpCollection", jsonArray);
				}
			}
			//Create VCP Collection
			if(item.getLabs()!=null){
				LabTypePresenter presenter = new LabTypePresenter();
				if(item.getLabs().size()>0){
					JSONArray jsonArray = new JSONArray();
					for(int i=0; i<item.getLabs().size(); i++){
						jsonArray.put(presenter.createJSON(item.getLabs().get(i)));
					}
					json.put("labCollection", jsonArray);
				}
			}
			return json;
		} catch(JSONException ex){
			ex.printStackTrace();
		}
		return null;
	}
	
	public PackageType getEmptyEntity(){
		PackageType entity = new PackageType();
		return entity;
	}
}
